package tyk.Service;

import tyk.Dao.BaseDao;
import tyk.pojo.*;

import java.awt.*;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/***
 *@title OrderService
 *@description <TODO description class purpose>
 *@author Tang'ya'kang
 *@VERSION 1.0.0
 *@CREATE 2023/5/18 17:52
 **/
public class OrderService extends BaseDao {
    public String CreateOrder(Cart cart, User user) throws SQLException {
        Date date = new Date();//获取当前的日期
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String str = df.format(date);//获取String类型的时间
        System.out.println(str);
        long order_id=System.currentTimeMillis();
        Order order=new Order(order_id+user.getId().toString(),user.getId(),new BigDecimal(String.valueOf(cart.getPriceTocal())),0,str);
        System.out.println("order->"+order);
        String sql="insert into t_orders(order_id,user_id,price,status,create_time) values(?,?,?,?,?);";
        int rows=super.executeUpdate(sql,order.getOrder_id(),order.getUser_id(),order.getPrice(),order.getStatus(),order.getCreate_time());
        return order.getOrder_id();
    }
    public List<Order> SelectMyOrderByUserid(Integer user_id) throws SQLException, NoSuchFieldException, InstantiationException, IllegalAccessException {
        String sql="select order_id,user_id,price,status,create_time from t_orders where user_id = ?;";
        List<Order> list = new ArrayList<>();
        list =super.executeQuery(Order.class,sql,user_id);
        System.out.println(list);
        return list;
    }
    public List<Order> SelectOrderRoot(AccountPage accountPage) throws SQLException, NoSuchFieldException, InstantiationException, IllegalAccessException {
        List<Order> list = new ArrayList<>();
        String sql="select order_id,user_id,price,status,create_time from t_orders;";
        list=super.executeQuery(Order.class,sql);
        accountPage.setCount(list.size());
        accountPage.setPageTocal(accountPage.getCount()/12);
        if(accountPage.getCount()%12!=0) accountPage.setPageTocal(accountPage.getPageTocal()+1);
        String sql2="select order_id,user_id,price,status,create_time from t_orders limit ?,?;";
        list=super.executeQuery(Order.class,sql2,(accountPage.getPage()-1)*12,12);
        return list;
    }


}
